//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		action:'sale.live_myrooms',
		ShowAuthM:false,
		pagetitle:'我的直播间' + '-' + app.setting.title,
		
	},
	onReady: function (){
		//获得粉丝消息弹窗(需要时开启)
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (){
		var page=this;
		var ShowAuthM=page.data.ShowAuthM;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		app.util.get(app.access,function(pagedata){
			//console.log('pagedata');console.log(pagedata);
			if((!pagedata.member.nickname || !pagedata.member.avatar) && !app.isgetUserInfo){
					ShowAuthM=!ShowAuthM
			}
			page.setData({
				pagedata:pagedata,
				ShowAuthM:ShowAuthM,
				pluginnav:app.session.menus,
				action:app.access.plugin+'/'+app.access.action+'/',
			});
			page.get_list(1);
		});
	},
	get_list:function(e){
		var the=this;
		var page = the.data.page
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		the.setData({
			loading:true
		});
		wx.showLoading({
		  title: '加载中...',
		})
		if(e){
			if(e.currentTarget){
				var data=e.currentTarget.dataset;
				if(data.type=='price'){
					pagedata.params.order='price';
					pagedata.params.order_type=pagedata.params.order_type?0:1;
				}else if(data.type=='sales'){
					pagedata.params.order='sales';
				}else if(data.type=='ishot'){
					pagedata.params.order='ishot';
				}else{
					pagedata.params.order='default';
				}
				if(data.group_id){
					pagedata.params.group_id=data.group_id
				}
			}
			page = 1;
			pagedata.list=[];
			isnodata=false;
		}else{
			if(isnodata){
				the.setData({
						loading:false
				});
				return false;
			}
		}
		var query='page='+page+'&num=8';
		var more_query=app.util.made_query(pagedata.params);
		if(more_query){
			query+='&'+more_query;
		}
		app.util.get('jy_weishop/sale.live_myrooms/getlist/'+query,function(res){
			console.log('res');console.log(res);
			wx.hideLoading()
			if(res.list.length==0){
				the.setData({
					isnodata:true,
					'pagedata.params':pagedata.params,
					loading:false,
				});
			}else{
				pagedata.list = pagedata.list.concat(res.list)
				page++
				the.setData({
					'pagedata.list':pagedata.list,
					'pagedata.params':pagedata.params,
					page:page,
					isnodata:isnodata,
					loading:false,
				});
			}
			
		});
	},
	noshowgoods:function(e){
		var the = this
		var pagedata = the.data.pagedata
		the.setData({
			'pagedata.showgoods':false,
		})
	},
	noshowcoupons:function(e){
		var the = this
		var pagedata = the.data.pagedata
		the.setData({
			'pagedata.showcoupons':false,
		})
	},
	noshowfeemodal:function(e){
		var the = this
		var pagedata = the.data.pagedata
		the.setData({
			'pagedata.showfeemodal':false,
		})
	},
	setgoods:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var data=e.currentTarget.dataset
		pagedata.post.id = data.id
		the.setData({
			'pagedata.showgoods':true,
			'pagedata.post':pagedata.post,
		})
		the.get_goods(1)
	},
	setcoupon:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var data=e.currentTarget.dataset
		pagedata.post.id = data.id
		the.setData({
			'pagedata.showcoupons':true,
			'pagedata.post':pagedata.post,
		})
		the.get_coupons(1)
	},
	set_feemodal:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var data=e.currentTarget.dataset
		the.setData({
			'pagedata.showfeemodal':true,
		})
		the.get_feemodal(1)
	},
	get_goods:function(e){
		var the=this;
		var page = the.data.page
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		the.setData({
			loading:true
		});
		wx.showLoading({
		  title: '加载中...',
		})
		if(e==1){
			page = 1;
			pagedata.goodslist=[];
			isnodata=false;
		}else{
			if(isnodata){
				the.setData({
						loading:false
				});
				return false;
			}
		}
		var query='page='+page+'&num=8';
		var more_query=app.util.made_query(pagedata.params);
		if(more_query){
			query+='&'+more_query;
		}
		app.util.get('jy_weishop/sale.get_data/get_goods/'+query,function(res){
			console.log('get_goods');console.log(res);
			wx.hideLoading()
			if(res.list.length==0){
				the.setData({
					isnodata:true,
					'pagedata.params':pagedata.params,
					loading:false,
				});
			}else{
				pagedata.goodslist = pagedata.goodslist.concat(res.list)
				page++
				the.setData({
					'pagedata.goodslist':pagedata.goodslist,
					'pagedata.params':pagedata.params,
					page:page,
					isnodata:isnodata,
					loading:false,
				});
			}
			
		});
	},
	get_coupons:function(e){
		var the=this;
		var page = the.data.page
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		the.setData({
			loading:true
		});
		wx.showLoading({
		  title: '加载中...',
		})
		if(e==1){
			page = 1;
			pagedata.coupons=[];
			isnodata=false;
		}else{
			if(isnodata){
				the.setData({
						loading:false
				});
				return false;
			}
		}
		var query='page='+page+'&num=8';
		var more_query=app.util.made_query(pagedata.params);
		if(more_query){
			query+='&'+more_query;
		}
		app.util.get('jy_weishop/sale.get_data/get_coupons/'+query,function(res){
			console.log('res');console.log(res);
			wx.hideLoading()
			if(res.list.length==0){
				the.setData({
					isnodata:true,
					'pagedata.params':pagedata.params,
					loading:false,
				});
			}else{
				pagedata.coupons = pagedata.coupons.concat(res.list)
				page++
				the.setData({
					'pagedata.coupons':pagedata.coupons,
					'pagedata.params':pagedata.params,
					page:page,
					isnodata:isnodata,
					loading:false,
				});
			}
			
		});
	},
	get_feemodal:function(e){
		var the=this;
		var page = the.data.page
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		the.setData({
			loading:true
		});
		wx.showLoading({
		  title: '加载中...',
		})
		if(e==1){
			page = 1;
			pagedata.redmodal.list=[];
			isnodata=false;
		}else{
			if(isnodata){
				the.setData({
						loading:false
				});
				return false;
			}
		}
		var query='page='+page+'&num=8';
		var more_query=app.util.made_query(pagedata.params);
		if(more_query){
			query+='&'+more_query;
		}
		app.util.get('jy_weishop/sale.get_data/get_feemodal/'+query,function(res){
			console.log('res');console.log(res);
			wx.hideLoading()
			if(res.list.length==0){
				the.setData({
					isnodata:true,
					'pagedata.params':pagedata.params,
					loading:false,
				});
			}else{
				pagedata.redmodal.list = pagedata.redmodal.list.concat(res.list)
				page++
				the.setData({
					'pagedata.redmodal.list':pagedata.redmodal.list,
					'pagedata.params':pagedata.params,
					page:page,
					isnodata:isnodata,
					loading:false,
				});
			}
			
		});
	},
	//翻页
	onReachBottom: function () {
		var the=this;
		var isnodata=the.data.isnodata
		var loading=the.data.loading
		var pagedata=the.data.pagedata
		if(!isnodata && !loading){
			if(pagedata.showgoods){
				the.get_goods()
			}
			if(pagedata.showcoupons){
				the.get_coupons()
			}
			if(pagedata.showfeemodal){
				the.get_feemodal()
			}
		}
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		app.util.getUserInfo(e,function(res){
			the.onLoad();
		});
		app.isgetUserInfo=true
		the.setData({
			ShowAuthM:false
		})
	},
	closeAuthM:function(){
		var the=this;
		the.setData({
			ShowAuthM:false
		})
	},
	goback: function() {
		var pages=getCurrentPages()
		if(pages.length>1){
			wx.navigateBack({
				'delta':1,
				'fail':function(){
					//console.log('333');
					app.util['access'](app.plugin_name+'/index/');
				},
			})
		}else{
			app.util['access'](app.plugin_name+'/index/');
		}
    },
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	click_tab:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util.tab(data.params);
	},
	setgoodstorooms:function(){
		var the = this
		var pagedata = the.data.pagedata
		app.util.post('jy_weishop/sale.live_myrooms/setgoodstorooms/',pagedata.post,function(res){
			app.util.msg(res);
			the.noshowgoods()
		});
	},
	checkcoupon:function(){
		var the = this
		var pagedata = the.data.pagedata
		the.noshowcoupons()
	},
	goselect:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var goodsids = pagedata.post.goods_ids;
		var value = ''
		var field = ''
		if(e){
			var res = e.currentTarget.dataset
			var value = res.id
			var field = res.field
		}
		if(goodsids==undefined || goodsids==''){
			goodsids=';';
		}
		if(goodsids.indexOf(';'+value+';')<0){
			goodsids=goodsids+value+';';
		}else{
			goodsids=goodsids.replace(';'+value+';',';');
			if(goodsids==';'){
				goodsids='';
			}
		}
		pagedata.post.goods_ids = goodsids
		the.setData({
				'pagedata.post':pagedata.post,
				'pagedata.isgoodsids':pagedata.isgoodsids,
				'pagedata.mygoods':res.mygoods
		})
	},
	bindPickerChange:function(e){
		var the = this
		var pagedata = the.data.pagedata
		pagedata.item.type = e.detail.value
		the.setData({
			'pagedata.item.type':pagedata.item.type
		})
	},
	chooseshareimg: function (e) {
		var the = this
		var pagedata = the.data.pagedata
		var value = e.currentTarget.dataset
		app.util.image(function(res){
			pagedata.item.shareimg = res
			the.setData({
				'pagedata.item.shareimg':pagedata.item.shareimg
			})
		})
	},
	choosecover_img: function (e) {
		var the = this
		var pagedata = the.data.pagedata
		var value = e.currentTarget.dataset
		app.util.image(function(res){
			pagedata.item.cover_img = res
			the.setData({
				'pagedata.item.cover_img':pagedata.item.cover_img
			})
		})
	},
});